﻿using System;
using System.IO;
using Neatly;
using UnityEditor;
using UnityEngine;

namespace NeatlyEditor
{
    public static class ConfigDataEditor
    {
        private const string PATH_CONFIG_DATA = "Assets/Config/ConfigData.asset";

        [MenuItem("ScriptObject/创建ConfigData", false)]
        static void CreateConfigData()
        {
            GetConfigData();
        }

        public static void UpdateConfigData()
        {
            var configData = GetConfigData();
            configData.isConsole = CommandParam.Console;
            configData.isDevelop = CommandParam.Develop;
            configData.serverType = (WebServerType)CommandParam.ServerType;
            configData.patchCode = CommandParam.PatchCode;
            EditorUtility.SetDirty(configData);
            AssetDatabase.SaveAssets();
            AssetDatabase.Refresh();
        }

        public static ConfigData GetConfigData()
        {
            if (!File.Exists(PATH_CONFIG_DATA))
            {
                AssetDatabase.Refresh();
                var scriptableObj = ScriptableObject.CreateInstance<ConfigData>();
                AssetDatabase.CreateAsset(scriptableObj, PATH_CONFIG_DATA);
                AssetDatabase.SaveAssets();
                AssetDatabase.Refresh();
                return scriptableObj;
            }
            return AssetDatabase.LoadAssetAtPath<ConfigData>(PATH_CONFIG_DATA);
        }

        private static int GetSeconds()
        {
            TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1);
            return Convert.ToInt32(ts.TotalSeconds);
        }
    }
}